<?php
/**------------------------------------------------------------------------
 * @Author:      lixw
 * @DateTime:    2017-01-09 17:52:55
 * @Description: 问题控制器
 * Copyright (c) 2016 All rights reserved.
 *------------------------------------------------------------------------*/
namespace app\wap\controller;

class Question extends Base{
	public $pagesize = 10;
	/**
	* 列表
	*/
	public function index(){
		$this->add_step('问答');
		$category_id = $this->_get('category_id');
		if($category_id){
			$where['category_id'] = $category_id;
		}
		// 默认
		$is_default = 1;
		// 热门
		$is_hot = $this->_get('is_hot');
		if($is_hot){
			$order['answer_num'] = 'desc';
			$order['view_num']   = 'desc';
			$url_params['is_hot'] = 1;
			$is_default = 0;
		}
		// 热门
		$unanswer = $this->_get('unanswer');
		if($unanswer){
			$where['answer_num'] = 0;
			$url_params['unanswer'] = 1;
			$is_default = 0;
		}
		$qwhere = $where ? $where : array();
		$url_params = $url_params ? $url_params : array();
		// 设置URL参数，用于分页
		$query = array_merge($qwhere, $url_params);

		$where['is_delete'] = 0;
		$where['status']    = 1;
		$order = $order ? $order : 'order_id,answer_time desc,ask_id desc';

		$page = $this->_get('page');
		$page = $page ? $page : 1;
		$query['page'] = $page;

		$list  = model('Ask')->where($where)->order($order)->paginate($this->pagesize, false, ['query' => $query]);

		$except  = $list->total() / $this->pagesize;
		$total_page = ceil($except);

		if($page >= $total_page){
			$is_hasmore = '';
		}else{
			$is_hasmore = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?page=' . ($page + 1);
		}

		$this->ajax_next_page($list, $page);


		$this->assign('list', $list);
		$this->assign('qwhere', $qwhere);
		$this->assign('query', $query);
		$this->assign('is_default', $is_default);
		$this->assign('category_list', model('Category')->getCategoryList('ask_index'));
		$this->assign('ajax_next_page', $is_hasmore);
		return $this->view();
	}

	// 提交问题
	public function submit(){
		// 设置需要登录才能访问
		$this->user_id(true);
		$this->add_step('提交问题');

		$this->is_header_nav   = true;
		$this->is_foot_bartab  = false;
		$this->is_header 	   = false;// 头部导航
		if($this->isPost()){
			$model     = model('Ask');
			$post_data = input('post.');
			$post_data['user_id']     = $this->_user_id;
			$post_data['create_time'] = time();
			// 验证器
			$validate = validate('Ask');
			if($validate && !$validate->scene('add')->check($post_data)){
				$this->error($validate->getError());
			}
			if($model->validate(true)->create($post_data, true) !== false){
				$ask_id = $model->getLastInsID();
				// 悬赏
				model('UserAccount')->ask_submit($this->_user_id, $post_data['reward'], $ask_id);
				// 更新用户问答数量
				model('UserData')->update_user_data($this->_user_id, 'question_num');
				$this->success('发布成功', url('Question/detail', array('ask_id' => $ask_id)));
			} else {
				$this->error('添加失败！');
			}
		}
		$reward_data = model('Config')->_get('reward_val_list', 'reward');
		$reward_list = explode(PHP_EOL, $reward_data);
		$reward_usable = model('UserAccount')->get_account_money($this->_user_id, 'beans_usable');

		$this->assign('reward_list', $reward_list);
		$this->assign('reward_usable', $reward_usable);
		return $this->view();
	}

	// 问题详情
	public function detail(){
		$this->is_foot_bartab = false;
		$ask_id = $this->_get('ask_id', '对不起，您的操作有误！');

		$ask_model = model('Ask');
		$info = $ask_model->find($ask_id);
		// 设置标题
		$this->add_step($info['title']);
		$is_fav = model('User')->is_fav($this->_user_id, $ask_id, 'ask_fav');

		$page          = $this->_get('page');
		$page          = $page ? $page : 1;
		$query['page'] = $page;

		$list = $this->get_answer_list($ask_id, $page);

		foreach ($list as $key => $value) {
			$module    = 'ask_answer';
			$object_id = $value['answer_id'];

			$where['module']    = $module;
			$where['object_id'] = $object_id;
			$list[$key]['comment_list']  = model('Comment')->where($where)->order('comment_id desc')->select();
			foreach ($list[$key]['comment_list'] as $k => $val) {
				if($val['reply_user_id'] > 0){
					$list[$key]['comment_list'][$k]['reply_user_name']  = model('Comment')->getReplyUserNameAttr($val['reply_user_id']);
				}
			}
			$list[$key]['comment_count'] = count($list[$key]['comment_list']);
		}
		$except  = $list->total() / $this->pagesize;
		$total_page = ceil($except);

		if($page >= $total_page){
			$is_hasmore = '';
		}else{
			$is_hasmore = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?page=' . ($page + 1);
		}

		$this->assign('info', $info);
		
		$this->ajax_next_page($list, $page, 'answer');
		$best = model("AskAnswer")->where(array('answer_id' => $info['answer_id']))->find();
		if(!empty($best)){
			$module    = 'ask_answer';
			$object_id = $best['answer_id'];
			$where['module']    = $module;
			$where['object_id'] = $object_id;
			$best['comment_list']  = model('Comment')->where($where)->order('comment_id desc')->select();
			$best['comment_count'] = count($best['comment_list']);
			foreach ($best['comment_list'] as $k => $val) {
				if($val['reply_user_id'] > 0){
					$best['comment_list'][$k]['reply_user_name']  = model('Comment')->getReplyUserNameAttr($val['reply_user_id']);
				}
			} 
		}

		$this->assign('best', $best);
		$this->assign('list', $list);
		$this->assign('is_fav', $is_fav);
		$this->assign('ajax_next_page', $is_hasmore);
		return $this->view();
	}

	// 回答列表
	public function get_answer_list($ask_id){
		$ask_id = $ask_id ? $ask_id : $this->_get('ask_id', '对不起，您的操作有误！');
		$model  = model('AskAnswer');
		$pagesize = 10;
		$where['ask_id']    = $ask_id;
		$where['is_best']   = 0;
		$where['is_delete'] = 0;
		$order = 'answer_id desc';
		$query['page'] = $page;

		$list  = $model->where($where)->order($order)->paginate($pagesize, false, ['query' => $query]);

		// $this->assign('list', $list);
		// $this->assign('page', $list->render());

		return $list;
	}

	// 答案点赞
	public function ask_fav(){
		// 设置需要登录才能访问
		$this->user_id(true);
		$ask_id = $this->_get('ask_id', '对不起，您的操作有误！');

		$model = model('User');
		$reid  = $model->set_fav('Ask', 'fav_num', 'ask_id', $this->_user_id, $ask_id, 'ask_fav');
		
		if($reid == 1){
			// 消息通知
			model('Message')->ask_push($ask_id, 'fav', $this->_user_id);
		}
		if($reid == 2){
			// 消息通知
			model('Message')->ask_push($ask_id, 'fav_cancel', $this->_user_id);
		}
		
		$data['node_id'] = 'ask-id-' . $ask_id;
		$data['node_data']['fav_num'] = $model->_total_fav_num;
		
		$this->success(model('User')->tips_info, '', $data);
	}
}